﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Indigo.Quibbler.Controls
{
    public partial class SettingsToolbar : UserControl
    {
        #region Button Click Events
        public event EventHandler ClickSettings;
        public event EventHandler ClickOffline;
        public event EventHandler ClickThemes;
        #endregion

        #region Dependency Property
        public string UpdateTime
        {
            get { return (string)GetValue(UpdateTimeProperty); }
            set { SetValue(UpdateTimeProperty, value); }
        }
        public static readonly DependencyProperty UpdateTimeProperty =
                   DependencyProperty.Register("UpdateTime", typeof(string), typeof(SettingsToolbar), new UIPropertyMetadata("0 SECONDS", OnUpdateTimeUpdated));


        public bool IsOnline
        {
            get { return (bool)GetValue(IsOnlineProperty); }
            set { SetValue(IsOnlineProperty, value); }
        }
        public static readonly DependencyProperty IsOnlineProperty =
            DependencyProperty.Register("IsOnline", typeof(bool), typeof(SettingsToolbar), 
            new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsOnlineChanged)));
        #endregion

        public SettingsToolbar()
        {
            InitializeComponent();
            btnSettings.Click += new RoutedEventHandler(btnSettings_Click);
            btnThemes.Click += new RoutedEventHandler(btnThemes_Click);
        }

        #region Public Methods
        public void FlashStatus()
        {
            (this.Resources["sbFlashStatus"] as Storyboard).Begin();
        }
        #endregion

        #region Event Handlers: Property Changed
        private static void OnUpdateTimeUpdated(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            (obj as SettingsToolbar).OnUpdateTimeUpdated(e);
        }
        
        private static void OnIsOnlineChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            (o as SettingsToolbar).OnIsOnlineChanged(e);
        }
        #endregion

        #region Event Handlers
        void btnThemes_Click(object sender, RoutedEventArgs e)
        {
            if (ClickThemes != null)
            {
                ClickThemes(sender, e);
            }
        }

        void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            if (ClickSettings != null)
            {
                ClickSettings(sender, e);
            }
        }

        void btnOffline_Click(object sender, RoutedEventArgs e)
        {
            if (ClickOffline != null)
            {
                ClickOffline(sender, e);
            }
        }
        #endregion

        #region Dependency Members
        private void OnUpdateTimeUpdated(DependencyPropertyChangedEventArgs e)
        {
            string newString = e.NewValue.ToString();
            if (newString != null)
            {
                textLastUpdate.Text = newString;
                if (newString.ToLower().Contains("updating"))
                {
                    cnvSpiral.Visibility = Visibility.Visible;
                    (this.Resources["sbAnimation"] as Storyboard).Begin();
                }
                else
                {
                    if (cnvSpiral.Visibility == Visibility.Visible)
                    {
                        cnvSpiral.Visibility = Visibility.Collapsed;
                        (this.Resources["sbAnimation"] as Storyboard).Stop();
                    }
                }
            }
        }

        private void OnIsOnlineChanged(DependencyPropertyChangedEventArgs e)
        {
            Color textColor = ((bool)e.NewValue) ? (Color)ColorConverter.ConvertFromString("#FF65D600") :
                (Color)ColorConverter.ConvertFromString("#FFDA3030");
            txtOnlineStatus.Foreground = new SolidColorBrush(textColor);
            txtOnlineStatus.Text = ((bool)e.NewValue) ? "ONLINE" : "OFFLINE";
        }
        #endregion

    }
}
